package com.lcredu.ajax.servlet;

import com.google.gson.Gson;
import com.lcredu.ajax.entity.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 黎长容
 * @date 2025-07-03 17:42
 */
public class CheckUserServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String uname = req.getParameter("uname");
        
        if ("king".equals(uname)){
            //用户名不可用,返回JSON字符串
            Gson gson = new Gson();
            User user = new User(200,"king","123456","163@.12");
            String json = gson.toJson(user);
            resp.getWriter().write(json);
        }else {
            //用户名可用,返回空串
            resp.getWriter().write("");  
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
